
#define GL_GLEXT_PROTOTYPES
#include <iostream>
#include <string.h>
#include <math.h>
#include <GL/glew.h>

#include "DisplayObject.h"
#include "Cube.h"

using namespace std;

namespace Krengine
{
	Cube::Cube( float width, float height, float depth )
		: DisplayObject(0.0, 0.0, 0.0),
		  cubeWidth(width), cubeHeight(height), cubeDepth(depth)
	{
		/* store vertexBufferArray size */
		bufferSize[vertices] = 24;

		/* generate vertexdata */
		vertBf = new GLfloat[24];
		GLfloat tmpVerts[24] = { -(cubeWidth / 2),   cubeHeight / 2,  -(cubeDepth / 2),
								   cubeWidth / 2,    cubeHeight / 2,  -(cubeDepth / 2),
								   cubeWidth / 2,    cubeHeight / 2,    cubeDepth / 2,
								 -(cubeWidth / 2),   cubeHeight / 2,    cubeDepth / 2,
								 -(cubeWidth / 2), -(cubeHeight / 2), -(cubeDepth / 2),
								   cubeWidth / 2,  -(cubeHeight / 2), -(cubeDepth / 2),
								   cubeWidth / 2,  -(cubeHeight / 2),   cubeDepth / 2,
								 -(cubeWidth / 2), -(cubeHeight / 2),   cubeDepth / 2 };

		/* register vertexdata */
		memcpy(vertBf, tmpVerts, sizeof(GLfloat) * bufferSize[vertices]);


		/* store normalBufferArray size */
		bufferSize[normal] = 24;

		/* generate normal data */
		normalBf = new GLfloat[24];
		GLfloat tmpNorm[24] = { -1.0f,  1.0f, -1.0f,
								 1.0f,  1.0f, -1.0f,
								 1.0f,  1.0f,  1.0f,
								-1.0f,  1.0f,  1.0f,
								-1.0f, -1.0f, -1.0f,
								 1.0f, -1.0f, -1.0f,
								 1.0f, -1.0f,  1.0f,
								-1.0f, -1.0f,  1.0f };

		/* register normaldata */
		memcpy(normalBf, tmpNorm, sizeof(GLfloat) * 24);


		/* store colorBufferArray size */
		bufferSize[color] = 24;

		/* generate colordata */
		colorBf = new GLfloat[24];
		GLfloat tmpColors[24];
		for ( int i = 0; i < 24; i++ )
			tmpColors[i] = 1.0f;

		/* register colordata */
		memcpy(colorBf, tmpColors, sizeof(GLfloat) * 24);


		/* store indexBufferArray size */
		bufferSize[index] = 36;

		/* generate indexdata */
		indexBf = new GLuint[36];
		GLuint tmpIndices[36] = { 0, 1, 5,
								  0, 5, 4,
								  1, 2, 6,
								  1, 6, 5,
								  3, 2, 6,
								  3, 6, 7,
								  0, 3, 7,
								  0, 7, 4,
								  4, 5, 6,
								  4, 6, 7,
								  0, 1, 2,
								  0, 2, 3 };

		/* register indexdata */
		memcpy(indexBf, tmpIndices, sizeof(GLuint) * 36);
	}


	/**
	 * empty destructor
	 */
	//TODO perform cleanup check
	Cube::~Cube() { }


	/**
	 * deletes any used resources and buffers
	 */
	void Cube::cleanup()
	{
		cout << "cleaning up cube...\n";

		/* delete used buffers */
		DisplayObject::cleanupShader();

		/* deallocate actual buffer information */
		delete[] vertBf;
		delete[] colorBf;
		delete[] indexBf;
	}
}

